<?php
 
/**
 */
require_once("log/LoggerLevel.php");
//require_once(LOG4PHP_DIR . '/spi/LoggerLoggingEvent.php');

/**
 * This class has been deprecated and replaced by the Logger subclass.
 *
 * @author VxR <vxr@vxr.it>
 * @version $Revision: 1.17 $
 * @package log4php
 * @see Logger
 */
class LoggerCategory {


    /**
     * @var LoggerLevel The assigned level of this category.
     */
    var $level          = null;
    
    /**
     * @var string name of this category.
     */
    var $name           = '';

    
    /**
     * Constructor.
     *
     * @param  string  $name  Category name   
     */
    function LoggerCategory($file)
    {
        $this->file = $file;
    }
    

    /**
     * Log a message object with the DEBUG level including the caller.
     *
     * @param mixed $message message
     * @param mixed $caller caller object or caller string id
     */
    function debug($message)
    {
        $debugLevel = LoggerLevel::getLevelDebug();
        if ($debugLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
            $this->forcedLog($this->name, $debugLevel, $message);
        }
    } 

    /**
     * Log a message object with the ERROR level including the caller.
     *
     * @param mixed $message message
     * @param mixed $caller caller object or caller string id
     */
    function error($message)
    {
        $errorLevel = LoggerLevel::getLevelError();
        if ($errorLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
            $this->forcedLog($this->name, $errorLevel, $message);
        }
    }


    /**
     * Log a message object with the ERROR level including the caller.
     *
     * @param mixed $message message
     * @param mixed $caller caller object or caller string id
     */
    function err($message)
    {
        $errorLevel = LoggerLevel::getLevelError();
        if ($errorLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
            $this->forcedLog($this->name, $errorLevel, $message);
        }
    }

    /**
     * Log a message object with the FATAL level including the caller.
     *
     * @param mixed $message message
     * @param mixed $caller caller object or caller string id
     */
    function fatal($message)
    {
        $fatalLevel = LoggerLevel::getLevelFatal();
        if ($fatalLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
            $this->forcedLog($this->name, $fatalLevel, $message);
        }
    } 
  
    /**
     * This method creates a new logging event and logs the event without further checks.
     *
     * It should not be called directly. Use {@link info()}, {@link debug()}, {@link warn()},
     * {@link error()} and {@link fatal()} wrappers.
     *
     * @param string $fqcn Fully Qualified Class Name of the Logger
     * @param mixed $caller caller object or caller string id
     * @param LoggerLevel $level log level
     * @param mixed $message message
     * @see LoggerLoggingEvent
     */
    function forcedLog($fqcn, $level, $message)
    {
        $fp = @fopen($this->file, 'a+');
        if ($fp) {
            fseek($fp, 0, SEEK_END);
            @fwrite($fp, $level->toString() . " " . $fqcn . " - " . $message . "\r\n");
            @fclose($fp);
        }
        
    }

    /**
     * Starting from this category, search the category hierarchy for a non-null level and return it.
     * @see LoggerLevel
     * @return LoggerLevel or null
     */
    function getEffectiveLevel()
    {
        return LoggerLevel::toLevel($this->level);
    }
  
    /**
     * Returns the assigned Level, if any, for this Category.
     * @return LoggerLevel or null 
     */
    function getLevel()
    {
        return $this->level;
    } 

    /**
     * Return the category name.
     * @return string
     */
    function getName()
    {
        return $this->name;
    }

    function setName($name)
    {
        $this->name = $name;
    }


    /**
     * Log a message object with the INFO Level.
     *
     * @param mixed $message message
     * @param mixed $caller caller object or caller string id
     */
    function info($message)
    {
        $infoLevel = LoggerLevel::getLevelInfo();
        if ($infoLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
            $this->forcedLog($this->name, $infoLevel, $message);
        }
    }


    /**
     * Check whether this category is enabled for the DEBUG Level.
     * @return boolean
     */
    function isDebugEnabled()
    {
        $debugLevel = LoggerLevel::getLevelDebug();
        return ($debugLevel->isGreaterOrEqual($this->getEffectiveLevel()));
    }       

    /**
     * Check whether this category is enabled for a given Level passed as parameter.
     *
     * @param LoggerLevel level
     * @return boolean
     */
    function isEnabledFor($level)
    {
        return (bool)($level->isGreaterOrEqual($this->getEffectiveLevel()));
    } 

    /**
     * Check whether this category is enabled for the info Level.
     * @return boolean
     * @see LoggerLevel
     */
    function isInfoEnabled()
    {
        $infoLevel = LoggerLevel::getLevelInfo();
        return ($infoLevel->isGreaterOrEqual($this->getEffectiveLevel()));
    } 

    /**
     * Set the level of this Category.
     *
     * @param LoggerLevel $level a level string or a level costant 
     */
    function setLevel($level)
    {
        $this->level = $level;
    } 

    /**
     * Log a message with the WARN level.
     *
     * @param mixed $message message
     * @param mixed $caller caller object or caller string id
     */
    function warn($message)
    {
        $warnLevel = LoggerLevel::getLevelWarn();
        if ($warnLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
            $this->forcedLog($this->name, $warnLevel, $message);
        }
    }

}  
?>